from manimlib import *

sys.path.append('.')
from customized import *


class p1(Scene):
    def construct(self):
        # self.add(NumberPlane())
        frame = self.camera.frame
        self.play(frame.animate.set_width(16))
        x, y = 26 / 5, 17 / 5
        a, b, c, d = [-x, y, 0], [x, y, 0], [x, -y, 0], [-x, -y, 0]
        s = CustomizedPolygon(a, b, c, d)
        o = [x - 6 / 5, -y, 0]
        # square
        s1 = CustomizedSquare().move_to([x - 1 / 5, -y + 1, 0]).set_fill('#75cfb8', 1)
        s2 = s1.copy().next_to(s1, UP, buff=0)
        s3 = s1.copy().next_to(s2, UP, buff=0)
        s4 = s1.copy().next_to(s3, LEFT, buff=0)
        s5 = s1.copy().next_to(s4, UP, buff=0)
        s6 = s1.copy().next_to(s5, UP, buff=0)
        s7 = s1.copy().next_to(s3, RIGHT, buff=0)
        vg = VGroup(s1, s2, s3, s4, s5, s6, s7).rotate(53 * DEGREES, about_point=o)
        bx, by = Brace(s, UP), Brace(s, LEFT)
        tx, ty = CustomizedTex('26').next_to(bx, UP), CustomizedTex('17').next_to(by, LEFT)
        ttt = CustomizedTex('?').move_to([0.6, 0.4, 0])
        self.add(s, vg, bx, by, tx, ty, ttt)
        self.wait()


class p2(Scene):
    def construct(self):
        # self.add(NumberPlane())
        frame = self.camera.frame
        self.play(frame.animate.set_width(16))
        x, y = 26 / 5, 17 / 5
        a, b, c, d = [-x, y, 0], [x, y, 0], [x, -y, 0], [-x, -y, 0]
        s = CustomizedPolygon(a, b, c, d)
        o = [x - 6 / 5, -y, 0]
        # square
        s1 = CustomizedSquare().move_to([x - 1 / 5, -y + 1, 0]).set_fill('#75cfb8', 1)
        s2 = s1.copy().next_to(s1, UP, buff=0)
        s3 = s1.copy().next_to(s2, UP, buff=0)
        s4 = s1.copy().next_to(s3, LEFT, buff=0)
        s5 = s1.copy().next_to(s4, UP, buff=0)
        s6 = s1.copy().next_to(s5, UP, buff=0)
        s7 = s1.copy().next_to(s3, RIGHT, buff=0)
        vg = VGroup(s1, s2, s3, s4, s5, s6, s7).rotate(53 * DEGREES, about_point=o)
        # triangle
        bx, by = Brace(s, UP), Brace(s, LEFT)
        tx, ty = CustomizedTex('26').next_to(bx, UP), CustomizedTex('17').next_to(by, LEFT)
        self.add(s, vg, bx, by, tx, ty)
        #
        p = [x, -y + 8 / 5, 0]
        t1 = CustomizedPolygon(o, [x, -y, 0], p).set_fill('#ffc478', 1)
        t2 = t1.copy().rotate(1.5 * PI).next_to(p, UL, buff=0)
        t3 = t2.copy().next_to(t2, UL, buff=0)
        t4 = t2.copy().next_to(t3, UL, buff=0)
        t5 = t1.copy().next_to(t4.point_from_proportion(0), DL, buff=0)
        t6 = t2.copy().next_to(t5.point_from_proportion(0), UL, buff=0)
        t7 = t2.copy().next_to(t6, UL, buff=0)
        t8 = t1.copy().next_to(t7.point_from_proportion(0), DL, buff=0)
        t9 = t1.copy().next_to(t5, UR, buff=0)
        self.add(t1, t2, t3, t4, t5, t6, t7, t8, t9, CustomizedTex('a').next_to(t1, DOWN), CustomizedTex('b').next_to(t1, RIGHT),
                 CustomizedTex('c').next_to(t1, UL, buff=-0.5))
        self.wait()


class p3(Scene):
    def construct(self):
        # self.add(NumberPlane())
        frame = self.camera.frame
        # self.play(frame.animate.set_width(16))
        side = 5
        x, y = side / 2, side / 5
        a, b, c, d = [-x, y, 0], [x, y, 0], [x, -y, 0], [-x, -y, 0]
        s = CustomizedSquare(5).set_fill('#75cfb8', 1)
        self.add(s)
        t = CustomizedPolygon(ORIGIN, [side, 0, 0], [4 * np.cos(37 * DEGREES), 4 * np.sin(37 * DEGREES), 0]).shift(
            DL * 2.5).set_fill(WHITE, 1)
        for i in range(4):
            self.add(t.copy().rotate_about_origin(i * PI / 2))

        self.wait()


class p4(Scene):
    def construct(self):
        # self.add(NumberPlane())
        frame = self.camera.frame
        self.play(frame.animate.set_width(16))
        x, y = 26 / 5, 17 / 5
        a, b, c, d = [-x, y, 0], [x, y, 0], [x, -y, 0], [-x, -y, 0]
        s = CustomizedPolygon(a, b, c, d)
        o = [x - 6 / 5, -y, 0]
        # square
        s1 = CustomizedSquare().move_to([x - 1 / 5, -y + 1, 0]).set_fill('#75cfb8', 1)
        s2 = s1.copy().next_to(s1, UP, buff=0)
        s3 = s1.copy().next_to(s2, UP, buff=0)
        s4 = s1.copy().next_to(s3, LEFT, buff=0)
        s5 = s1.copy().next_to(s4, UP, buff=0)
        s6 = s1.copy().next_to(s5, UP, buff=0)
        s7 = s1.copy().next_to(s3, RIGHT, buff=0)
        vg = VGroup(s1, s2, s3, s4, s5, s6, s7).rotate(53 * DEGREES, about_point=o)
        # triangle
        bx, by = Brace(s, UP).set_color(BLACK), Brace(s, LEFT).set_color(BLACK)
        tx, ty = CustomizedTex('26').next_to(bx, UP), CustomizedTex('17').next_to(by, LEFT)
        self.add(s, vg, bx, by, tx, ty)
        #
        p = [x, -y + 8 / 5, 0]
        t1 = CustomizedPolygon(o, [x, -y, 0], p).set_fill('#ffc478', 1)
        t2 = t1.copy().rotate(1.5 * PI).next_to(p, UL, buff=0)
        t3 = t2.copy().next_to(t2, UL, buff=0)
        t4 = t2.copy().next_to(t3, UL, buff=0)
        t5 = t1.copy().next_to(t4.point_from_proportion(0), DL, buff=0)
        t6 = t2.copy().next_to(t5.point_from_proportion(0), UL, buff=0)
        t7 = t2.copy().next_to(t6, UL, buff=0)
        t8 = t1.copy().next_to(t7.point_from_proportion(0), DL, buff=0)
        t9 = t1.copy().next_to(t5, UR, buff=0)
        self.add(t1, CustomizedTex('a').next_to(t1, DOWN), CustomizedTex('b').next_to(t1, RIGHT), CustomizedTex('c').next_to(t1, UL, buff=-0.5))
        for i in [t2, t3, t4, t5, t6, t7, t8, t9]:
            self.play(TransformFromCopy(t1, i))
        self.wait()